package wireless.frequent.action;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import wireless.frequent.pojo.Frequent;

import cm.util.hibernate.HibernateSessionFactory;
import wireless.util.*;

public class FrequentBarChart {
	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
    private int length;
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
    public String execute() throws Exception{
    	DefaultCategoryDataset dataset=new DefaultCategoryDataset();
    	session=HibernateSessionFactory.getSession();
		trans=session.beginTransaction();
    	length = getLength();
    	if(16==length)
    	{
    		hql="from Frequent as f where f.length>8";
    		query=session.createQuery(hql);
    	}
    	else{
    		hql="from Frequent as f where f.length=:l";
    		query=session.createQuery(hql);
    		query.setInteger("l",length);//设置查询参数
    	}
    	
		l=query.list();
		  for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
		     {
			  Frequent frequent=(Frequent)sit.next();
			  String value = frequent.getValue();
			  if(length!=4)
			  {
				value=Long.toHexString(Long.parseLong(value, 2)).toUpperCase();
			  }
			  int count = frequent.getCount();
			  if(count>500)
			  {
				  dataset.addValue(count, "出现次数", value);//设置条形图的值
			  }
		     }
		  JFreeChart chart = ChartFactory.createBarChart3D("频繁串柱形图分析（大于500）", // chart title
				    "频繁串", // domain axis label
				    "出现次数", // range axis label
				    dataset, // data
				    PlotOrientation.VERTICAL, // orientation
				    true, // include legend
				    true, // tooltips
				    false // urls
				    );
		  	chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
			CategoryPlot plot = chart.getCategoryPlot(); 
			NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
			vn.setAutoRangeIncludesZero(true); 

			CategoryAxis domainAxis = plot.getDomainAxis(); 

			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
			// Lable 
			Font labelFont = new Font("宋体", Font.BOLD, 12); 

			domainAxis.setLabelFont(labelFont);
			domainAxis.setTickLabelFont(labelFont);

			domainAxis.setMaximumCategoryLabelWidthRatio(1f);
			// domainAxis.setVerticalCategoryLabels(false); 
			plot.setDomainAxis(domainAxis); 

			ValueAxis rangeAxis = plot.getRangeAxis(); 
			rangeAxis.setLabelFont(labelFont);
			rangeAxis.setTickLabelFont(labelFont);
			rangeAxis.setUpperMargin(0.15); 
			rangeAxis.setLowerMargin(0.15); 
			plot.setRangeAxis(rangeAxis); 
			BarRenderer renderer = new BarRenderer(); 
			renderer.setMaximumBarWidth(0.7); 
			renderer.setMinimumBarLength(10); 

			renderer.setBaseOutlinePaint(Color.BLACK); 

			renderer.setSeriesPaint(0, Color.GREEN); 
			renderer.setSeriesPaint(1, new Color(0, 0, 255)); 
			renderer.setItemMargin(0); 
			renderer 
			.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
			renderer.setBaseItemLabelsVisible(true); 

			plot.setRenderer(renderer); 
			plot.setForegroundAlpha(0.6f); 
			plot.setBackgroundAlpha(0.7f);
			
			chart.getLegend().setItemFont(labelFont);
	        
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
						450, 300);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		  
		  session.close();
		  System.out.println("成功");
				return null;
		 } 	  
		
    }

